/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.master.demo.im.component.handler;

import com.farsunset.cim.constant.ChannelAttr;
import com.farsunset.cim.group.SessionGroup;
import com.farsunset.cim.group.TagSessionGroup;
import com.farsunset.cim.handler.CIMRequestHandler;
import com.farsunset.cim.model.SentBody;
import com.master.common.validator.ValidatorUtil;
import com.master.demo.im.component.handler.annotation.CIMHandler;
import com.master.demo.im.pojo.entity.Session;
import com.master.demo.im.service.ISessionService;
import io.netty.channel.Channel;
import io.netty.util.AttributeKey;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * @author: Yang
 * @date: 2022/07/05
 * @version: 1.0.0
 * Copyright Ⓒ 2022 Master Computer Corporation Limited All rights reserved.
 */
@CIMHandler(key = "client_closed")
public class ClosedHandler implements CIMRequestHandler {
    @Autowired
    private ISessionService sessionService;
    @Autowired
    private SessionGroup sessionGroup;
    @Autowired
    private TagSessionGroup tagsessionGroup;

    @Override
    public void process(Channel channel, SentBody body) {
        String uid = channel.attr(ChannelAttr.UID).get();
        if (uid == null) {
            return;
        }
        if (ValidatorUtil.isNull(channel.attr(ChannelAttr.TAG).get())) {
            closeSession(channel, uid);
        } else {
            closeMember(channel, uid);
        }
    }

    /**
     * 退出会话
     *
     * @param channel
     * @param uid
     */
    private void closeSession(Channel channel, String uid) {
        String nid = channel.attr(ChannelAttr.ID).get();
        sessionGroup.remove(channel);
        sessionService.updateStatus(uid, nid, Session.STATUS_ACTIVE);
    }

    /**
     * 退出会话
     *
     * @param channel
     * @param uid
     */
    private void closeMember(Channel channel, String uid) {
        tagsessionGroup.remove(channel);
        String roomId = channel.attr(ChannelAttr.TAG).get();

        Object name = channel.attr(AttributeKey.valueOf("name")).get();

        if (roomId == null || name == null) {
            return;
        }
        //User user = new User();
        //user.setName(name.toString());
        //user.setRoomId(Long.parseLong(roomId));
        //user.setUid(uid);
        //userService.remove(user);
    }

}
